﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCvoucherAsp;

namespace MVCvoucherAsp.Controllers
{ 
    public class BtcVoucherController : Controller
    {
        private VoucherDbEntities1 db = new VoucherDbEntities1();
        public int bctIdee;
        //
        // GET: /BtcVoucher/
        public ViewResult login() 
        {
            return View();
        }
        
        public ViewResult Index()
        {
            return View(db.BTC.ToList());
        }

        //
        // GET: /BtcVoucher/Details/5

        public ViewResult Details(int id)
        {
            BTC btc = db.BTC.Single(b => b.BtcId == id);
            return View(btc);
        }
        public ViewResult detailVoucher(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            return View(voucher);
        }

        
        //
        // GET: /BtcVoucher/Edit/5
 
        public ActionResult Edit(int id)
        {
            BTC btc = db.BTC.Single(b => b.BtcId == id);
            return View(btc);
        }

        public ViewResult VoucherbyBtc(int id) 
        {
            {
                var voucher = db.Voucher.Where(b => b.BtcId == id);
                return View(voucher.ToList());
            }
        }
        //
        // POST: /BtcVoucher/Edit/5
        //[HttpPost]
        //public ActionResult login()
        //{
        //    if (ModelState.IsValid) 
        //    {
        //      var voucher = db.BTC.Select(v => v.BtcId == bt   );

                


        //    }
       // }

        [HttpPost]
        public ActionResult Edit(BTC btc)
        {
            if (ModelState.IsValid)
            {
                db.BTC.Attach(btc);
                db.ObjectStateManager.ChangeObjectState(btc, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(btc);
        }

        // GET: /Voucher/Delete/5

        public ActionResult Delete(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            return View(voucher);
        }

        //
        // POST: /Voucher/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            db.Voucher.DeleteObject(voucher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult editVoucher(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }

        //
        // POST: /Voucher/Edit/5

        [HttpPost]
        public ActionResult editVoucher(Voucher voucher)
        {
            if (ModelState.IsValid)
            {
                db.Voucher.Attach(voucher);
                db.ObjectStateManager.ChangeObjectState(voucher, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }      


        public ActionResult newVoucher()
        {
            
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam");
            return View();
        }


        [HttpPost]
        public ActionResult newVoucher(Voucher voucher)
        {
            
            if (ModelState.IsValid)
            {
                db.Voucher.AddObject(voucher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}